﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using appDataContracts;
using appClient;
using appInterfaces;
using System.ServiceModel;
using System.Web.UI.HtmlControls;

namespace appClient
{
	public partial class Catalog : System.Web.UI.Page
	{

		IClient client;
		int colNum;
		int idSupplierGET;
		int idCatalogGET;
		int idProductGET;

		protected void Page_Load(object sender, EventArgs e)
		{
			client = new ChannelFactory<IClient>("clientHttp").CreateChannel();
			colNum = 1;

			if (Request.QueryString["s"] != null)
			{
				idSupplierGET = int.Parse(Request.QueryString["s"].ToString());
				showCatalogsFromSupplier(idSupplierGET);
			}
			else
			{
				if (Request.QueryString["c"] != null)
				{
					idCatalogGET = int.Parse(Request.QueryString["c"].ToString());
					showProductsFromCatalog(idCatalogGET);
				}
				else
				{
					if (Request.QueryString["p"] != null)
					{
						if (Session["user"] != null)
						{
							idProductGET = int.Parse(Request.QueryString["p"].ToString());
							addToCart(idProductGET);
						}
						else
						{
							Response.Redirect("Account/Login.apsx");
						}
					}
					else
					{
						idSupplierGET = 0;
						showCatalogsFromSupplier(0);
					}
				}
			}
		}

		// column number where to add catalogs ... dynamic generation
		private void updateColNum()
		{
			if (colNum == 1)
				this.colNum++;
			else
				this.colNum--;
		}

		public void showCatalogsFromSupplier(int supplierID)
		{
			List<SerializedCatalog> catalogs = new List<SerializedCatalog>();

			if (supplierID == 0)
				catalogs = client.getCatalogs();
			else
				catalogs = client.getSupplierCatalogs(supplierID);

			foreach (SerializedCatalog c in catalogs)
			{
					HtmlGenericControl boxDiv = new HtmlGenericControl("div");
					HtmlGenericControl leftTopDiv = new HtmlGenericControl("div");
					HtmlGenericControl rightTopDiv = new HtmlGenericControl("div");
					HtmlGenericControl rightBotDiv = new HtmlGenericControl("div");
					HtmlGenericControl leftBotDiv = new HtmlGenericControl("div");
					HtmlGenericControl innerDiv = new HtmlGenericControl("div");

					HtmlGenericControl titleDiv = new HtmlGenericControl("div");
					HtmlGenericControl midDiv = new HtmlGenericControl("div");
					HtmlGenericControl imgDiv = new HtmlGenericControl("div");
					HtmlGenericControl descDiv = new HtmlGenericControl("div");
					HtmlGenericControl footDiv = new HtmlGenericControl("div");

					HtmlGenericControl catalogTitle = new HtmlGenericControl("h3");
					HtmlGenericControl img = new HtmlGenericControl("img");
					HtmlGenericControl pImg = new HtmlGenericControl("p");
					HtmlGenericControl descriptionP = new HtmlGenericControl("p");
					HtmlGenericControl seeMoreLink = new HtmlGenericControl("a");

					// adding title
					if (idSupplierGET != 0)
						titleH2.InnerText = "Catalogues de " + client.getSupplier(idSupplierGET).SUPPLIERNAME;
					else
						titleH2.InnerText = "Liste des catalogues";

					// adding attributes to div
					boxDiv.Attributes.Add("class", "box1");
					leftTopDiv.Attributes.Add("class", "left-top-corner");
					rightTopDiv.Attributes.Add("class", "right-top-corner");
					rightBotDiv.Attributes.Add("class", "right-bot-corner");
					leftBotDiv.Attributes.Add("class", "left-bot-corner");
					innerDiv.Attributes.Add("class", "inner");

					titleDiv.Attributes.Add("class", "titleDiv");
					midDiv.Attributes.Add("class", "midDiv");
					imgDiv.Attributes.Add("class", "imgDiv");
					descDiv.Attributes.Add("class", "descDiv");
					footDiv.Attributes.Add("class", "footDiv");

					img.Attributes.Add("src", "Upload/" + c.CATALOGIMAGE);

					// adding attributes to a-link
					seeMoreLink.Attributes.Add("href", "Catalog.aspx?c="+c.CATALOGID);
					seeMoreLink.Attributes.Add("class", "link1");

					// adding div to div...
					boxDiv.Controls.Add(leftTopDiv);
					leftTopDiv.Controls.Add(rightTopDiv);
					rightTopDiv.Controls.Add(rightBotDiv);
					rightBotDiv.Controls.Add(leftBotDiv);
					leftBotDiv.Controls.Add(innerDiv);

					innerDiv.Controls.Add(titleDiv);
					innerDiv.Controls.Add(midDiv);
						midDiv.Controls.Add(imgDiv);
						midDiv.Controls.Add(descDiv);
					innerDiv.Controls.Add(footDiv);

					// filling other elements
					catalogTitle.InnerHtml = c.CATALOGNAME;
					descriptionP.InnerText = c.CATALOGDESCRIPTION.ToString();
					seeMoreLink.InnerText = "Voir";

					// adding other elements to div
					titleDiv.Controls.Add(catalogTitle);
					imgDiv.Controls.Add(img);
					descDiv.Controls.Add(descriptionP);
					footDiv.Controls.Add(seeMoreLink);

					if (colNum == 1)
						c1.Controls.Add(boxDiv);
					else
						c2.Controls.Add(boxDiv);


				// last call
				updateColNum();
			}
		}

		public void showProductsFromCatalog(int catalogID)
		{
			List<SerializedProduct> products = new List<SerializedProduct>();

			products = client.getProducts(catalogID);

			foreach (SerializedProduct p in products)
			{
				HtmlGenericControl boxDiv = new HtmlGenericControl("div");
				HtmlGenericControl leftTopDiv = new HtmlGenericControl("div");
				HtmlGenericControl rightTopDiv = new HtmlGenericControl("div");
				HtmlGenericControl rightBotDiv = new HtmlGenericControl("div");
				HtmlGenericControl leftBotDiv = new HtmlGenericControl("div");
				HtmlGenericControl innerDiv = new HtmlGenericControl("div");

				HtmlGenericControl titleDiv = new HtmlGenericControl("div");
				HtmlGenericControl midDiv = new HtmlGenericControl("div");
					HtmlGenericControl imgDiv = new HtmlGenericControl("div");
					HtmlGenericControl descDiv = new HtmlGenericControl("div");
				HtmlGenericControl mid2Div = new HtmlGenericControl("div");
				HtmlGenericControl footDiv = new HtmlGenericControl("div");
					HtmlGenericControl priceDiv = new HtmlGenericControl("div");
					HtmlGenericControl moreInfoDiv = new HtmlGenericControl("div");
					HtmlGenericControl orderDIv = new HtmlGenericControl("div");

				HtmlGenericControl productTitle = new HtmlGenericControl("h3");
				HtmlGenericControl img = new HtmlGenericControl("img");
				HtmlGenericControl pImg = new HtmlGenericControl("p");
				HtmlGenericControl priceP = new HtmlGenericControl("p");
				HtmlGenericControl seeMoreLink = new HtmlGenericControl("a");
				HtmlGenericControl moreInfoLink = new HtmlGenericControl("a");

				// adding title
				if(idCatalogGET != 0)
					titleH2.InnerText = "Produits du catalogue "+ client.getCatalog(idCatalogGET).CATALOGNAME;
				else
					titleH2.InnerText = "Produits";

				// adding attributes to div
				boxDiv.Attributes.Add("class", "box");
				leftTopDiv.Attributes.Add("class", "left-top-corner");
				rightTopDiv.Attributes.Add("class", "right-top-corner");
				rightBotDiv.Attributes.Add("class", "right-bot-corner");
				leftBotDiv.Attributes.Add("class", "left-bot-corner");
				innerDiv.Attributes.Add("class", "inner");
				titleDiv.Attributes.Add("class", "titleDiv");
				midDiv.Attributes.Add("class", "midDiv");
				imgDiv.Attributes.Add("class", "imgDiv");
				descDiv.Attributes.Add("class", "descDiv");
				mid2Div.Attributes.Add("class", "mid2Div");
				footDiv.Attributes.Add("class", "footDiv");
				priceDiv.Attributes.Add("class", "priceDiv");
				moreInfoDiv.Attributes.Add("class", "moreInfoDiv");
				orderDIv.Attributes.Add("class", "orderDIv");

				img.Attributes.Add("src", "Upload/"+p.PRODUCTIMAGE);

				// adding attributes to a-link
				seeMoreLink.Attributes.Add("href", "Catalog.aspx?p=" + p.PRODUCTID.ToString());
				seeMoreLink.Attributes.Add("class", "button");
				moreInfoLink.Attributes.Add("href", "About.aspx?c="+idCatalogGET+"&p=" + p.PRODUCTID.ToString()); 
				moreInfoLink.Attributes.Add("class", "button");

				// adding div to div...
				boxDiv.Controls.Add(leftTopDiv);
				leftTopDiv.Controls.Add(rightTopDiv);
				rightTopDiv.Controls.Add(rightBotDiv);
				rightBotDiv.Controls.Add(leftBotDiv);
				leftBotDiv.Controls.Add(innerDiv);

				innerDiv.Controls.Add(titleDiv);
				innerDiv.Controls.Add(midDiv);
					midDiv.Controls.Add(imgDiv);
					midDiv.Controls.Add(descDiv);
				innerDiv.Controls.Add(mid2Div);
					mid2Div.Controls.Add(priceDiv);
				innerDiv.Controls.Add(footDiv);
					footDiv.Controls.Add(moreInfoDiv);
					footDiv.Controls.Add(orderDIv);

				// filling other elements
				productTitle.InnerHtml = p.PRODUCTNAME;
				descDiv.InnerText = p.PRODUCTDESCRIPTION;
				priceP.InnerHtml = "<h5>"+p.PRODUCTPRICE.ToString()+"€</h5>";
				moreInfoLink.InnerText = "+ d'infos";
				seeMoreLink.InnerText = "Commander";

				// adding other elements to div
				titleDiv.Controls.Add(productTitle);
				imgDiv.Controls.Add(img);
				priceDiv.Controls.Add(priceP);
				moreInfoDiv.Controls.Add(moreInfoLink);
				orderDIv.Controls.Add(seeMoreLink);

				if (colNum == 1)
					c1.Controls.Add(boxDiv);
				else
					c2.Controls.Add(boxDiv);


				// last call
				updateColNum();
			}
		}

		public void addToCart(int productID)
		{
			List<CaddieProduct> newList = new List<CaddieProduct>(((List<CaddieProduct>)Session["caddie"]));// copying the list to be able to do some modifications in the loop...
			int i=0;
			bool found = false;

			/* principe :
			 * remplir une liste temporaire (newList), et remplacer celle de la session par la nouvelle (newList)
			*/

			// check if already added any
			if (Session["caddie"] != null && ((List<CaddieProduct>)Session["caddie"]).Count != 0)
			{
				foreach (CaddieProduct cp in (List<CaddieProduct>)Session["caddie"])
				{
					if (cp.Id == productID)
					{
						newList[i].Quantity++;
						found=true;
					}
					i++;
				}
				if(found == false)
					newList.Add(new CaddieProduct(productID, 1));
			}
			else
			{
				newList.Add(new CaddieProduct(productID, 1));
			}

			// updating the real list (session)
			((List<CaddieProduct>) Session["caddie"]).Clear();
			Session["caddie"] = newList;

			HtmlGenericControl paragraph = new HtmlGenericControl();
			HtmlGenericControl paragraph2 = new HtmlGenericControl();
			paragraph.InnerHtml = "L'article " + client.getProductInfo(productID).PRODUCTNAME + " a bien été ajouté à votre <a href=\"MyCaddie.aspx\">Panier</a>";
			paragraph2.InnerHtml = "<br /><br /><br /><a href=\"Catalog.aspx\">Revenir à la liste des catalogues</a>";

			wrapperServer.Controls.Add(paragraph);
			wrapperServer.Controls.Add(paragraph2);
		}


	}
}